package com.bjsxt.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Controller
public class AkiPayController {
    // 应用标识
    private final String APP_ID = "2016102600765183";
    // 应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQD0t3beK4HL7FpiHvaMwH8Vt4J+Tu+QPKN2x+rdDDvJrMTWTCfmzBqF/Gbbe9dOQ+d8OOivpyI50ptJwJtv7G4woj00r9EIyX+f8w/3ECx7r6hKIKYL1viEPDy/PJHiCJESQoHigEdDeyOV9rSP7WXh1ywyCjJJzfAECaHJSiVquRWE0TQwsx5kNcCfL1HaaDYcyPLSBZXe3v9xmQn6Zjtr4dv/zVE+AcoAGpmXOB8wc3c3Q1w6rmmxaWPOiXql2P4X+Ff6LQsuohwaMkPFhX/o05liws5vXUd5KeqC74dBgPqgtRPpW/PFErkKCEpIdnoeiYc/fuUshZelDb9d5mJJAgMBAAECggEBAITmvl+aMTuWeN+hATUfteUNbIk8RU6IIB56JEs4QaAdfCkF4wWtAxqRIStB7oRKzITiQzxiAGx2vG8pc0Etngo7khHzCgtDo1QypREAb6gclcDsSkFT1lO6gyM4ueGP1XmdNPzVvd4gGgtvmFAyFYq5WP80GmuKtDb9JCg1ZGy4V+CkF9CSHnbvopc7B5os1Hkw9vK7F7KUQ33mTcYGlz8O3XWjh/+gVZCJEt+0ZynUbEZxhPCh+tc538OQVPl1nloPMyMYzd1aDKirVZEV6/oglWqS8wna/1yzqHUT2gvQBjKsmTSXoB3ciFn+cKw1P3O9Ov5SnjGawR/GAUwUXAECgYEA+8e+CR0JeyP6Mix/KF8Ql1sDVzfYO7PZoxp3btZSy1U02kZ3a21k7JBCDOG9/QovoNlPgn8bJ2Foq58qFLavUpKGorFsjHq2v8BJCBRJHOTGSY/CQnX/ZCH/BbqtK1SIRY5iesiJ+f9Uq49VzMvBiWa6Jd7SGQcf9MJ2PKsJsLkCgYEA+NFqdGh21f6XjbVHu8VsWDnSJphDfPCH4KtNBefqByaSGkL3bq6pQarHFxbmNB1vd5OKAhxsrnsf6fn5UQixPO4DiYlXlzqCKUrzUMcxA8j7a3qPF9JeOXtIFmCYysCI8rxzio/PlSa5HfJVbVtKWRcfMuqeNQNTbwXBc0M/1hECgYEA+YlgnfWqZOImYT3alO+LKQcqC3IsnBdG9zEO98nw6tc6965pQw57vT3ISK4fNz+2LqvR8pr5FZGkRJwN/9ODdcoFKA/CboRcxTTU7O31/G383AFf3bDG+r362/nFETHJkW5pyhkhLqXRi+sWdU/oM76ddVUeU3HNSS7GMqCtjPkCgYEAoG3mRchVVwUgkq4f4mIYyYn+hgQG/7fW5HWO5A/0RUgJTAWBpEvZtg128NxC8bdI98CZa2MIcDfTC/mxVx5TqQWOIk/7ufZvllBNMfg9EkO0WEhdmu548ncas9L0sUYTBu+bT+wJ3BFrqnNlwWzrMyZFZ37/Iw9R7HO5l6f0sQECgYBAtayOKpf5azEIw1cZX+1/nisVHuq/BbUc9M3IyrRIbECTrwu94xl/FEVRzY0kdN6vLoPksOrLQmaMVi9/sjgNwGr0NNtzkGSzFWAX1haE2tJVFkiNCfJPhRKLMJEZnzG3eBwfKNTqLvcctxD+eXloJewwUB0/EKyIQVKYegWUZA==";
    // 编码格式
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqABAAoKXPUXhNtN+dxms6VEEL3iE+5UTE94GaM9u33n0ND+GX5+85sH3FySVCFZ0y7EMrc1wz0UZKV1tLec6CaL5Vgy9+gxnVdOoUYHXTpo+G8yRfvUjfSnIvmBqzH2D+GSA7q2/razfvR8srlozNeciz3iXf6Icw+NXb6TrGYN+QpFJAr4hZO4y1WzL/AfDQowSkdttAKUqJI8H5j2fdekdzUmHnBigrWzbV/3QCFibmm9pwvcLWb4EbIFhssdNRsLQSyiGaSQui0OT41kr2OPoaHYY34qzHCRht0YtV9zoH73x8cTefvHYoF4Xn4wDyhSpVqAPqOw57mcq0K4WzQIDAQAB";
    // 沙箱路径接口，正是路径应为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    // 参数返回格式
    private final String FORMAT = "JSON";
    // 签名方式
    private final String SIGN_TYPE = "RSA2";
    // 支付宝异步通知路径，付款完毕后会异步调用本项目的方法，必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyURL";
    // 支付宝同步通知路径，也就是当付款完毕后跳转本项目的页面，可以不是公网地址
    private final String RETURN_URL = "http://127.0.0.1/returnURL";


    @RequestMapping("alipay")
    public void alipay(HttpServletResponse httpResponse) throws IOException {
        // 实例化客户端,填入所需参数
        // 网关地址 应用标识 应用私钥 传输格式 编码格式 支付宝公钥 签名类型
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Integer.toString(100);
        //订单名称，必填
        String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}

